from celery import shared_task
from UiManager.models import TestSuiteInfo, EnvInfo
from UiManager.utils.report import add_test_reports, timestamp_to_datetime
from UiManager.utils.runner import UiRunner
from UiManager.utils.emails import send_email_reports


@shared_task
def suite_delay_run(env, suite, email):
    kwargs = {
        "failfast": False,
        "verbosity": 2
    }
    runner = UiRunner(**kwargs)
    env_obj = EnvInfo.objects.get(id=env)
    suite_obj = TestSuiteInfo.objects.get(id=suite)
    # 运行信息
    run_list = {
        "suite": suite,
        "env": {
            "base_url": env_obj.base_url,
            "capabilities": env_obj.capabilities,
            "rule": env_obj.rule
        },
    }

    # 异步执行case
    runner.run(run_list)
    runner.summary = timestamp_to_datetime(runner.summary)
    report_path = add_test_reports(runner, html_report_name=suite_obj.suite_name)

    if email != '':
        send_email_reports(email, suite_obj.suite_name, report_path, runner)